package com.zzh.lib.core.wifi;


import android.Manifest;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.util.Log;

import androidx.annotation.RequiresPermission;

import com.zzh.lib.core.HLibrary;
import com.zzh.lib.core.lang.HStrUtils;

import java.util.List;

/**
 * Created by zzh on 2025/7/1.
 *
 * @date: 2025/7/1 10:17
 * @email: zzh_hz@126.com
 * @author: zzh
 * @description: com.zzh.lib.core.wifi Wifi 扫描结果广播接收器
 */
public class ScanWifiResultReceiver extends BroadcastReceiver {
    @RequiresPermission(allOf = {Manifest.permission.ACCESS_FINE_LOCATION})
    @Override
    public void onReceive(Context context, Intent intent) {
        if (HStrUtils.equals(intent.getAction(), WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
            WifiManager wifiManager = (WifiManager) HLibrary.getAppContext().getSystemService(Context.WIFI_SERVICE);
            @SuppressLint("MissingPermission") List<ScanResult> scanResults = wifiManager.getScanResults();
            HLibrary.getInstance().runScanWifiResultCallback(scanResults);
        } else if (HStrUtils.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION, intent.getAction())) {
            //网络连接状态改变。
            NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (networkInfo != null && networkInfo.isConnected()) {
                // WiFi 连接成功
                Log.d("WifiState", "已连接到 WiFi");
            } else {
                // WiFi 连接断开
                Log.d("WifiState", "WiFi 连接断开");
            }

        }
    }
}
